/**
 * Created by wangcan on 17/10/26.
 */
'use strict';
angular.module('backofficeApp.orderTotalCommisionListManager.controller', [])
    .controller('orderTotalCommisionListCtrl', ['$scope','$http','$routeParams','$location', function ($scope, $http,$routeParams,$location) {

        $scope.currentPage = 1;
        $scope.pageSize = 15;
        $(".alert").css("display","block");

        $scope.startdate = new Date(new Date().getTime() - 86400000 * 29).format("yyyy-MM-dd");
        $scope.enddate = new Date().format("yyyy-MM-dd");

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/commission/orderTatolCommisionList/:page/:size',
            ajax: $http,
            success:function(data){
                $(".alert").css("display","none");
                if(data && data.data) {
                    $("#startDateSelect").val($scope.startdate);
                    $("#endDateSelect").val($scope.enddate);
                    $scope.orderCommissionCountList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.currentPage = $(".am-active").text();
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $(".alert").css("display","none");
                //$scope.communityList = null;
            },
            params: {startdate: $scope.startdate, enddate: $scope.enddate}
        });

        //搜索
        $scope.search = function (current) {
            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            }else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            var startDateSelect = $("#startDateSelect").val();
            var endDateSelect = $("#endDateSelect").val();
            if(startDateSelect == '' && endDateSelect != '') {
                alert('开始日期不能为空！');
                return;
            } else if (startDateSelect != '' && endDateSelect == '') {
                alert('结束日期不能为空！');
                return;
            } else if (endDateSelect > $scope.enddate) { // 当前系统时间
                alert('结束时间不能大于当前时间！');
                return;
            } else {
                if(startDateSelect > endDateSelect) {
                    alert('开始日期不能大于结束日期！');
                    return;
                } else {
                    var startTemp = new Date(startDateSelect);
                    var endTemp = new Date(endDateSelect);
                    var sufDays = (endTemp.getTime() - startTemp.getTime()) / 86400000;
                    if(sufDays >= 92) {
                        alert('只能导出3个月及以内的数据！');
                        return;
                    }
                }
            }
            $(".alert").css("display","block");
            var option = $scope.option;
            var opt = {
                startdate:startDateSelect,
                enddate:endDateSelect
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/commission/orderTatolCommisionList/' + $scope.currentPage  + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $(".alert").css("display","none");
                $scope.orderCommissionCountList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = opt;
                $('#loading').modal("close");
            }).error(function (err) {
                $(".alert").css("display","none");
                $('#loading').modal("close");
                alert(err);
            });
        }

        // 导出
        $scope.exportdata = function (option, target) {
            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;
            var startDateSelect = $("#startDateSelect").val();
            var endDateSelect = $("#endDateSelect").val();

            if(!startDateSelect || !endDateSelect){
                alert('请选择开始或结束日期');
                return;
            }
            if (startDateSelect > endDateSelect) {
                alert('开始日期不能大于结束日期');
                return;
            } else if (endDateSelect > $scope.enddate) { // 当前系统时间
                alert('结束时间不能大于当前时间！');
                return;
            } else {
                var startTemp = new Date(startDateSelect);
                var endTemp = new Date(endDateSelect);
                var sufDays = (endTemp.getTime() - startTemp.getTime()) / 86400000;
                if(sufDays >= 92) {
                    alert('只能导出3个月及以内的数据！');
                    return;
                }
            }
            // $(".alert").css("display","block");
            var href = 'http://' + backOfficeHost + '/rest/v1.0/commission/orderTotalCommisionDataExport/?isValid=true';
            if (startDateSelect)  href += "&startdate=" + startDateSelect;
            if (endDateSelect)  href += "&enddate=" + endDateSelect;

            location.href = href;
        }

        // 跳转到大使用金页面
        // $scope.toAmbCommission = function (orderDate) {
        //     var url = "#/commission/ambCommissionForDay";
        //     // // http://localhost:3008/#/%23/commission/ambCommissionForDay?orderDate=2017-10-31
        //     $location.path(url).search({orderDate: orderDate});
        //     var curUrl = $location.absUrl();
        //     // alert("跳转到大使用金页面..." + curUrl);
        //     // location.href = href;
        // }
    }])